package edu.ynmd.cms.action;

import edu.ynmd.cms.model.*;
import edu.ynmd.cms.service.ManageService;
import edu.ynmd.cms.tools.OssTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("manage")
public class AdminAction {

    @Autowired
    public ManageService manageService;

    @PostMapping("addNews")
    @ResponseBody
    public boolean addNews(@RequestBody News news) throws Exception{
        news.setPbdate(System.currentTimeMillis());
        return manageService.addNews(news);
    }

    @PostMapping("deleteNews")
    @ResponseBody
    public boolean deleteNews(@RequestBody News news) throws Exception{
        return manageService.deleteNews(news.getNewid());
    }

    @PostMapping("updateNews")
    @ResponseBody
    public boolean updateNews(@RequestBody News news) throws Exception{
        return manageService.updateNews(news);
    }

    @PostMapping("getSingleNews")
    @ResponseBody
    public News getSingleNews(@RequestBody News news) throws Exception{
        return manageService.getNews(news.getNewid());
    }

    @GetMapping("getNewsList")
    @ResponseBody
    public List<News> getNewsList() throws Exception{
        return manageService.getNewsList();
    }

    @PostMapping("uploadNewCoverpic")
    @ResponseBody
    public HashMap uploadNewCoverpic(MultipartFile file, HttpServletRequest request)throws Exception{
        HashMap m = new HashMap();
        String filename = file.getOriginalFilename();
        String newsid = request.getHeader("newsid");

        //获得文件的后缀
        int index=filename.lastIndexOf(".");
        String sufixname=filename.substring(index);
        String savefilename=String.valueOf(System.currentTimeMillis())+sufixname;

        //检测文件夹的存在
        String savepath="D:\\JAVA EE\\springbootupload\\";
//        String savepath="/root/project/picupload/";
        File f=new  File(savepath);
        if (!f.exists()){
            f.mkdir();
        }
        //将文件保存到文件夹
//        file.transferTo(new File(savepath+savefilename));

        //oss存储
        String savefile=savepath+filename;
        File touploadfile=new File(savefile);
        file.transferTo(touploadfile);

        OssTools ossTools =new OssTools();
        ossTools.init();
        String osspath="/coverpic/"+savefilename;
        String result= ossTools.uploadFileForUrl(osspath,touploadfile);
        System.out.println("上传文件的结果"+result);
        String osshost="https://cms-1259279381.cos.ap-chengdu.myqcloud.com/coverpic/";
        String url =osshost+savefilename;

        //保存图片地址到数据库
        News toupdatenews=manageService.getNews(newsid);
        if (toupdatenews!=null){
            toupdatenews.setCoverpic(url);
            manageService.updateNews(toupdatenews);
        }

        m.put("filename",savefilename);
        m.put("newsid",newsid);
        return m;
    }


    @PostMapping("uploadFile")
    @ResponseBody
    public Map uploadFile(MultipartFile file) throws Exception{
        Map m=new HashMap();

        String filename=file.getOriginalFilename();
        System.out.printf("接收到的文件名是:"+filename);
        //获得文件的后缀
        int index=filename.lastIndexOf(".");
        String sufixname=filename.substring(index);
        String savefilename=String.valueOf(System.currentTimeMillis())+sufixname;

        //检测文件夹的存在
//      String savepath="/root/project/picupload/";
        String savepath="D:\\JAVA EE\\springbootupload\\";
        File f=new  File(savepath);
        if (!f.exists()){
            f.mkdir();
        }
        //将文件保存到文件夹
//        file.transferTo(new File(savepath+savefilename));
//        m.put("pic",savefilename);
//        return m;

        //oss存储
        String savefile=savepath+filename;
        File touploadfile=new File(savefile);
        file.transferTo(touploadfile);

        OssTools ossTools =new OssTools();
        ossTools.init();
        String osspath="/carousel/"+savefilename;
        String result= ossTools.uploadFileForUrl(osspath,touploadfile);
        System.out.println("上传文件的结果"+result);

        // String url="https://localhost/public/"+finename;

        String osshost="https://cms-1259279381.cos.ap-chengdu.myqcloud.com/carousel/";
        String url =osshost+savefilename;


        m.put("pic",url);
        return m;
    }

    @RequestMapping("/ckeditorUpload")
    @ResponseBody
    public String ckeditorUpload(@RequestParam("upload") MultipartFile file, String CKEditorFuncNum) throws Exception {
        if (!file.isEmpty()) {
            String finename=file.getOriginalFilename();
            String suffixname=file.getOriginalFilename().substring(finename.lastIndexOf("."));
            finename=String.valueOf(System.currentTimeMillis())+suffixname;
//            String filepath="/root/project/picupload/";
            String filepath="D:/JAVA EE/springbootupload/";
            File tf=new File(filepath);
            if(!tf.exists()){
                tf.mkdir();
            }
            String savefile=filepath+finename;
            try {

                File touploadfile=new File(savefile);
                file.transferTo(touploadfile);

                OssTools ossTools =new OssTools();
                ossTools.init();
                String osspath="/news/"+finename;
                String result= ossTools.uploadFileForUrl(osspath,touploadfile);
                System.out.println("上传文件的结果"+result);

                // String url="https://localhost/public/"+finename;

                String osshost="https://cms-1259279381.cos.ap-chengdu.myqcloud.com/news/";
                String url =osshost+finename;
//                String url="https://119.23.28.201/"+finename;
//                String url="https://localhost/public/"+finename;


                return "{\"uploaded\":1,\"fileName\":\""+savefile+"\",\"url\":\"" + url + "\"}";

            } catch (IOException e) {
                e.printStackTrace();
                return "{\"uploaded\":0,\"error\":{\"message\":\"upload file is not success!\"}}";
            } catch (IllegalStateException e) {
                e.printStackTrace();
                return "{\"uploaded\":0,\"error\":{\"message\":\"upload file is not success!\"}}";
            }

        }

        return "{\"uploaded\":0,\"error\":{\"message\":\"upload file is not success!\"}}";

    }


    @PostMapping("addCarousel")
    @ResponseBody
    public boolean addCarousel(@RequestBody Carousel carousel) throws Exception{
        return manageService.addCarousel(carousel);
    }

    @GetMapping("getCarouselList")
    @ResponseBody
    public List<Carousel> getCarouselList() throws Exception{
        return manageService.getCarouselList();
    }

    @PostMapping("getCarousel")
    @ResponseBody
    public Carousel getCarousel(@RequestBody Carousel carousel) throws Exception{
        return manageService.getCarousel(carousel.getCarouselid());
    }

    @PostMapping("deleteCarousel")
    @ResponseBody
    public boolean deleteCarousel(@RequestBody Carousel carousel) throws Exception{
        return manageService.deleteCarousel(carousel.getCarouselid());
    }


    @PostMapping("updateCarousel")
    @ResponseBody
    public boolean updateCarousel(@RequestBody Carousel carousel) throws Exception{
        return manageService.updateCarousel(carousel);
    }

    @PostMapping("addSinglepagelist")
    @ResponseBody
    public boolean addSinglepagelist(@RequestBody Singlepagelist singlepagelist) throws Exception{
        return manageService.addSinglepagelist(singlepagelist);
    }

    @GetMapping("getSinglepagelistList")
    @ResponseBody
    public List<Singlepagelist> getSinglepagelistList() throws Exception{
        return manageService.getSinglepagelistList();
    }


    @PostMapping("getSinglepagelist")
    @ResponseBody
    public Singlepagelist getSinglepagelist(@RequestBody Singlepagelist singlepagelist) throws Exception{
        return manageService.getSinglepagelist(singlepagelist.getSinglepageid());
    }

    @PostMapping("deleteSinglepagelist")
    @ResponseBody
    public boolean deleteSinglepagelist(@RequestBody Singlepagelist singlepagelist) throws Exception{
        return manageService.deleteSinglepagelist(singlepagelist.getSinglepageid());
    }


    @PostMapping("updateSinglepagelist")
    @ResponseBody
    public boolean updateSinglepagelist(@RequestBody Singlepagelist singlepagelist) throws Exception{
        return manageService.updateSinglepagelist(singlepagelist);
    }

    //科目表
    @PostMapping("addBook")
    @ResponseBody
    public boolean addBook(@RequestBody Book book) throws Exception{
        return manageService.addBook(book);
    }

    @PostMapping("deleteBook")
    @ResponseBody
    public boolean deleteBook(@RequestBody Book book) throws Exception{
        return manageService.deleteBook(book.getId());
    }

    @PostMapping("updateBook")
    @ResponseBody
    public boolean updateBook(@RequestBody Book book) throws Exception{
        return manageService.updateBook(book);
    }

    @PostMapping("getOneBook")
    @ResponseBody
    public Book getOneBook(@RequestBody Book book) throws Exception{
        return manageService.getBook(book.getId());
    }

    //课程表
    @PostMapping("addCourse")
    @ResponseBody
    public boolean addCourse(@RequestBody Course course) throws Exception{
        return manageService.addCourse(course);
    }

    @PostMapping("deleteCourse")
    @ResponseBody
    public boolean deleteCourse(@RequestBody Course course) throws Exception{
        return manageService.deleteCourse(course.getId());
    }

    @PostMapping("updateCourse")
    @ResponseBody
    public boolean updateCourse(@RequestBody Course course) throws Exception{
        return manageService.updateCourse(course);
    }

    @PostMapping("getOneCourse")
    @ResponseBody
    public Course getOneCourse(@RequestBody Course course) throws Exception{
        return manageService.getCourse(course.getId());
    }

    //班级表
    @PostMapping("addStudent")
    @ResponseBody
    public boolean addStudent(@RequestBody Student student) throws Exception{
        return manageService.addStudent(student);
    }

    @PostMapping("deleteStudent")
    @ResponseBody
    public boolean deleteStudent(@RequestBody Student student) throws Exception{
        return manageService.deleteStudent(student.getId());
    }

    @PostMapping("updateStudent")
    @ResponseBody
    public boolean updateStudent(@RequestBody Student student) throws Exception{
        return manageService.updateStudent(student);
    }

    @PostMapping("getOneStudent")
    @ResponseBody
    public Student getOneStudent(@RequestBody Student student) throws Exception{
        return manageService.getStudent(student.getId());
    }

    //教室表
    @PostMapping("addClassroom")
    @ResponseBody
    public boolean addClassroom(@RequestBody Classroom classroom) throws Exception{
        return manageService.addClassroom(classroom);
    }

    @PostMapping("deleteClassroom")
    @ResponseBody
    public boolean deleteClassroom(@RequestBody Classroom classroom) throws Exception{
        return manageService.deleteClassroom(classroom.getId());
    }

    @PostMapping("updateClassroom")
    @ResponseBody
    public boolean updateClassroom(@RequestBody Classroom classroom) throws Exception{
        return manageService.updateClassroom(classroom);
    }

    @PostMapping("getClassroom")
    @ResponseBody
    public Classroom getOneStudent(@RequestBody Classroom classroom) throws Exception{
        return manageService.getClassroom(classroom.getId());
    }

    //教师表
    @PostMapping("addTeacher")
    @ResponseBody
    public boolean addTeacher(@RequestBody Teacher teacher) throws Exception{
        return manageService.addTeacher(teacher);
    }

    @PostMapping("deleteTeacher")
    @ResponseBody
    public boolean deleteTeacher(@RequestBody Teacher teacher) throws Exception{
        return manageService.deleteTeacher(teacher.getId());
    }

    @PostMapping("updateTeacher")
    @ResponseBody
    public boolean updateTeacher(@RequestBody Teacher teacher) throws Exception{
        return manageService.updateTeacher(teacher);
    }

    @PostMapping("getTeacher")
    @ResponseBody
    public Teacher getTeacher(@RequestBody Teacher teacher) throws Exception{
        return manageService.getTeacher(teacher.getId());
    }
}
